package com.yboot.starter.springdoc.config;

import com.yboot.starter.springdoc.core.OpenApiHandler;
import com.yboot.starter.springdoc.core.TagOrderOpenApiCustomizer;
import io.swagger.v3.oas.models.OpenAPI;
import org.springdoc.core.OpenAPIService;
import org.springdoc.core.PropertyResolverUtils;
import org.springdoc.core.SecurityService;
import org.springdoc.core.SpringDocConfigProperties;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.customizers.OpenApiBuilderCustomizer;
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
import org.springdoc.core.providers.JavadocProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;

import java.util.List;
import java.util.Optional;

@AutoConfiguration
@ConditionalOnProperty(
    name = {"springdoc.api-docs.enabled"},
    matchIfMissing = true
)
@ConditionalOnWebApplication
public class SpringDocAutoConfiguration {
    public SpringDocAutoConfiguration() {
    }

    @Bean
    public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI,
                                         SecurityService securityParser,
                                         SpringDocConfigProperties springDocConfigProperties,
                                         PropertyResolverUtils propertyResolverUtils,
                                         Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomisers,
                                         Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomisers,
                                         Optional<JavadocProvider> javadocProvider) {
        return new OpenApiHandler(openAPI,
                securityParser,
                springDocConfigProperties,
                propertyResolverUtils,
                openApiBuilderCustomisers,
                serverBaseUrlCustomisers,
                javadocProvider);
    }

    @Bean
    public GlobalOpenApiCustomizer tagOrderOpenApiCustomizer() {
        return new TagOrderOpenApiCustomizer();
    }
}